package com.hr.mes.controller;

import com.hr.mes.dto.Page;
import com.hr.mes.dto.Result;
import com.hr.mes.entity.Permission;
import com.hr.mes.enums.MyExceptionEnum;
import com.hr.mes.exception.MyException;
import com.hr.mes.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequiresRoles("admin")
@Api(tags = "权限管理")
@CrossOrigin
@RequestMapping("/api/permission")
@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation("新增权限")
    @PostMapping("addPermission")
    public Result addPermission(@RequestBody Permission permission){
        try{
            permissionService.addPermission(permission);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PERMISSION_ADD_ERROR, e);
        }
    }

    @ApiOperation("删除权限")
    @GetMapping("detelePermission")
    public Result detelePermission(@RequestParam Integer permissionId){
        try {
            permissionService.deletePermission(permissionId);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PERMISSION_DELETE_ERROR, e);
        }
    }

    @ApiOperation("修改权限")
    @PostMapping("updatePermission")
    public Result updatePermission(@RequestBody Permission permission){
        try{
            permissionService.updatePermission(permission);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PERMISSION_UPDATE_ERROR, e);
        }
    }

    @ApiOperation("查询所有权限")
    @GetMapping("getPermissionPage")
    public Result getPermissionPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize){
        try {
            Page<Permission> permissionPage = permissionService.getPermissionPage(pageNo, pageSize);
            return Result.ok(permissionPage);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PERMISSION_VIEW_ERROR, e);
        }
    }

    @ApiOperation("查询一个权限")
    @GetMapping("getPermission")
    public Result getPermission(@RequestParam Integer permissionId){
        try {
            Permission permission = permissionService.getPermission(permissionId);
            return Result.ok(permission);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.PERMISSION_VIEW_ERROR, e);
        }
    }

}
